# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=fftw
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgver=3.3.10
pkgver=${_pkgver//-/.}
pkgrel=2
pkgdesc="A library for computing the discrete Fourier transform (DFT) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.fftw.org/"
license=("GPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || \
               echo "${MINGW_PACKAGE_PREFIX}-gcc-fortran"))
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('staticlibs' 'strip')
source=("https://www.fftw.org/${_realname}-${_pkgver}.tar.gz")
sha256sums=('56c932549852cddcfafdab3820b0200c7742675be92179e59e6215b340e26467')

precision="double float long_double \
           $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "quad" )"

build() {
  # clang complains when trying to use an sse2 intrinsic if sse2 is not enabled
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-i686* ]]; then
    CFLAGS+=" -msse2"
  fi

  for cur in ${precision}; do
    [[ -d ${srcdir}/${MSYSTEM}-${cur} ]] && rm -rf ${srcdir}/${MSYSTEM}-${cur}
    mkdir -p ${srcdir}/${MSYSTEM}-${cur} && cd ${srcdir}/${MSYSTEM}-${cur}

    msg "Building ${_realname} with ${cur} ..."

    local -a _config=""
    if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-aarch64* ]]; then
      _config+="--enable-sse2 --enable-avx --enable-avx-128-fma --enable-avx2"
    fi
    if [ $CARCH = x86_64 ]; then
      _config+=" --enable-avx512"
    fi

    if [ "${cur}" = "float" ]; then
      _config+=" --enable-float"
    elif [ "${cur}" = "long_double" ]; then
      _config="--enable-long-double"
    elif [ "${cur}" = "quad" ]; then
      _config="--enable-quad-precision"
    fi

    ../${_realname}-${_pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --build=${MINGW_CHOST} \
      --host=${MINGW_CHOST} \
      ${_config} \
      --enable-static \
      --enable-shared \
      --enable-threads \
      --with-combined-threads \
      --with-our-malloc \
      --with-g77-wrappers \
      --with-windows-f77-mangling

    make
  done
}

package() {
  for cur in ${precision}; do
    msg "Installing ${_realname} with ${cur} ..."
    make DESTDIR="${pkgdir}" install -C ${srcdir}/${MSYSTEM}-${cur}
  done
}
